/**
 * @author poppy
 */
var Utils = Utils ||
{};

Utils.format = function(str, json){
    if (arguments.length > 1) {
        var m = Utils.format, k = /([.*+?^=!:${}()|[\]\/\\])/g, l = (m.left_delimiter || "{").replace(k, "\\$1"), r = (m.right_delimiter || "}").replace(k, "\\$1");
        var r1 = m._r1 || (m._r1 = new RegExp("#" + l + "([^" + l + r + "]+)" + r, "g")), r2 = m._r2 || (m._r2 = new RegExp("#" + l + "(\\d+)" + r, "g"));
        if (typeof(json) == "object") {
            return str.replace(r1, function(a, key){
                var v = json[key];
                if (typeof v == 'function') {
                    v = v(key);
                }
                return typeof(v) == 'undefined' ? "" : v;
            });
        }
        else 
            if (typeof(json) != "undefined") {
                var vs = Array.prototype.slice.call(arguments, 1);
                var vl = vs.length;
                return str.replace(r2, function(a, index){
                    index = parseInt(index, 10);
                    return (index >= vl) ? a : vs[index];
                });
            }
    }
    return str;
};

Utils.delimiter = function(left, right){
    var me = Utils.format;
    me.left_delimiter = left || "{";
    me.right_delimiter = right || left || "}";
    me._r1 = me._r2 = null;
};

Utils.C = function(n){
    if (!Utils.G("const_" + n)) {
        return null;
    }
    return Utils.G("const_" + n).value;
}

Utils.G = function(obj){
    return typeof(obj) != "object" ? document.getElementById(obj + "") : obj;
}

Utils.encode = function(str){
    return encodeURIComponent(str);
}

Utils.encodeBeforeSubmit = function(form, input){
    input = typeof(input) == "string" ? Utils.G(input) : input;
    if (!input.value || input.value == "") {
        return true;
    }
    else {
        var candidate = document.createElement("input");
        candidate.type = "hidden";
        candidate.name = input.name;
        cadidate.value = Utils.encode(input.value);
        form.appendChild(cadidate);
        input.name = "";
        return true;
    }
}

Utils.post = function(url, params){
    var div = document.createElement('div');
    div.id = "__form-holder";
    div.style.display = "none";
    var temp = [];
    temp.push("<form id='__temp_form' method='POST' action='" + url + "'>");
    for (var k in params) {
        temp.push("<input type='hidden' name='" + k + "' value='" + params[k] + "'/>");
    }
    temp.push("</form>");
    div.innerHTML = temp.join("");
    document.body.appendChild(div);
    Utils.G("__temp_form").submit();
}

Utils.moveUp = function(id){
    var obj = Utils.G(id), prev = obj.previousSibling;
    while (prev && !prev.innerHTML) {
        prev = prev.previousSibling;
    }
    if (prev && prev.innerHTML) {
        prev.parentNode.insertBefore(obj, prev);
    }
}

Utils.moveDown = function(id){
    var obj = Utils.G(id), next = obj.nextSibling;
    while (next && !next.innerHTML) {
        next = next.nextSibling;
    }
    if (next && next.innerHTML) {
        obj.parentNode.insertBefore(next, obj);
    }
}

Utils.toggleMore = function(dom, str1, str2){
    var obj = Utils.G(dom), parent = obj.parentNode, next = parent.nextSibling, prev = parent.previousSibling;
    
	if(obj.innerHTML == str1){
		showMore();
	}else{
		hideMore();
	}
	
    function showMore(){
        next.style.display = "inline";
        obj.innerHTML = str2;
        Utils.moveDown(parent);
    }
    
    function hideMore(){
        prev.style.display = "none";
		obj.innerHTML = str1;
        Utils.moveUp(parent);
    }
}
Utils.nl2br = function(str) {
	return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1'+ '<br/>' +'$2');
}
